package com.yanqu.road.server.manger.cricket.skillhandler.startround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.startround.CricketSkillStartRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill409Strategy implements CricketSkillStartRoundStrategy {

    /**
     * 技能处理-诸葛亮-空城计：提高自身{0}的基础气势，当自身气势高于对方当前气势的{1}时，对方不攻自破。
     * @param cricketGame 本局对局对象
     * @param skillData 技能数据
     * @param action 我方
     * @param target 对方
     * @param roundId 第几回合
     * @param attackRoundBuilder 返回PB结构体
     * @return
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, BattleData action, BattleData target, int roundId, CricketProto.CricketAttackRound.Builder attackRoundBuilder) {
        skillData.effectAmountInc();
        //提升自己气势
        long addPower = BigDecimal.valueOf(action.getPower()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
        action.addPower(addPower);
        CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder = cricketGame.newBattleFrameBuilder();
        newBattleFrameBuilder.setActionUserId(action.getUserId());
        newBattleFrameBuilder.setTargetUserId(target.getUserId());
        newBattleFrameBuilder.setSkillId(skillData.getSkillId());

        //判断气势是否大于对方p2，对方对方不攻自破。
        BigDecimal diffPowerRate = BigDecimal.valueOf(action.getPower()).divide(BigDecimal.valueOf(target.getPower()), 8, RoundingMode.DOWN).multiply(BigDecimal.valueOf(1000));
        if (diffPowerRate.compareTo(BigDecimal.valueOf(skillData.getSkillEffect().getP2())) >= 0) {
            //不攻自破,血量减少成0
            AddHpResult reHpResult = target.addHp(-target.getMaxHp() * 10);
            newBattleFrameBuilder.setAttack(reHpResult.getValue());
            newBattleFrameBuilder.setIsKill(reHpResult.isKill());
            newBattleFrameBuilder.setHarmEffectSkillId(reHpResult.getReduceHpEffectSkillId());
            newBattleFrameBuilder.setIsMiss(reHpResult.isMissFlag());
        }
        cricketGame.fillRemainHp(newBattleFrameBuilder);
        attackRoundBuilder.addStartRound(newBattleFrameBuilder);
        //是否结束
        if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.StartRound.getValue(), newBattleFrameBuilder.getBattleFrameId())) {
            return true;
        }
        return false;
    }
}
